<?php

namespace app\modules\admin\controllers;

use YII;
use yii\web\Controller;

class DefaultController extends Controller
{
    public $defaultAction = 'index';

    public $layout = 'main';

    public function actionDb()
    {
        // YII::$app->db->createCommand("INSERT INTO `user`(`name`, `age`) values('oldlee', 28), ('louder', 28)")->execute();

        // $pst = YII::$app->db->createCommand("SELECT * FROM `user`")->query();

        YII::$app->db->open();

        $pst = YII::$app->db->pdo->prepare('SELECT * FROM `user` where `name`=:name');

        $pst->bindValue(':name', 'oldlee');

        $pst->execute();


        foreach ($pst as $f) {
            echo sprintf('name: %10s age: %3d', $f['name'], $f['age']), '<br/>';
        }
        
        // return $this->render('index');
    }

    public function actionIndex()
    {
        $data = ['name' => 'lee'];

        $key = md5(serialize($data));


        YII::$app->cache->set($key, $data, 30);

        if (false === ($cache = YII::$app->cache->get($key))) {
            return $this->asJson(['error' => 'no cache']);
        }
        return $this->asJson($cache);
    }

    public function actionView()
    {
        return $this->render('index');
    }
}
